using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using System.Web;
using System.Xml;
using System.Xml.Schema;
using Sedna.Core;
using Sedna.Core.Event;

namespace Sedna.Core.Event
{
    public abstract class BaseMessageEvent: BaseEvent
    {
        //protected const String BUSINESS_TX_SCHEMA = "~/xsd/BusinessTransaction.xsd";

        protected Object eventSource;

        #region Constructors
        
        public BaseMessageEvent()
        {}

        public BaseMessageEvent(String name) : base(name)
        {}

        public BaseMessageEvent(String name, Object eventSource)
            : this(name)
        {
            this.eventSource = eventSource;
        }

        #endregion
        
        public static XmlQualifiedName GetSchema(XmlSchemaSet schemaSet)
        {
            schemaSet.Add(GetSchemaSetNewInstance());
            schemaSet.Compile();

            return EventTypeNameNewInstance();
        }

        public override XmlSchemaSet ValidateSchemaSet()
        {
            return GetSchemaSetNewInstance();
        }

        public override void WriteEventSourceXml(XmlWriter writer)
        {
            if (eventSource != null)
            {

                writer.WriteStartElement(MESSAGES_PREFIX, "messages", MESSAGES_NS);

                writer.WriteStartElement(MESSAGES_PREFIX, "message", MESSAGES_NS);

                writer.WriteAttributeString("generated", DateTime.Now.ToString("s", DateTimeFormatInfo.InvariantInfo));

                writer.WriteStartElement(MESSAGES_PREFIX, "address", MESSAGES_NS);

                WriteAddress(writer);
                
/*                writer.WriteStartElement(MESSAGES_PREFIX, "mail", MESSAGES_NS);
                writer.WriteElementString(REFERENCED_PREFIX, "street1", REFERENCED_NS, "street1");
                writer.WriteElementString(REFERENCED_PREFIX, "street2", REFERENCED_NS, "street2");
                writer.WriteElementString(REFERENCED_PREFIX, "city", REFERENCED_NS, "city");
                writer.WriteElementString(REFERENCED_PREFIX, "state", REFERENCED_NS, "state");
                writer.WriteElementString(REFERENCED_PREFIX, "zip", REFERENCED_NS, "zip");
                writer.WriteElementString(REFERENCED_PREFIX, "country", REFERENCED_NS, "country");
                writer.WriteEndElement();*/

                writer.WriteEndElement();//address

                WriteContent(writer);
                //writer.WriteElementString(MESSAGES_PREFIX, "content", MESSAGES_NS, "Content");

                writer.WriteEndElement();

                writer.WriteEndElement();
            }
        }

        protected abstract void WriteAddress(XmlWriter writer);

        protected abstract void WriteContent(XmlWriter writer);

        private static XmlSchemaSet GetSchemaSetNewInstance()
        {
            XmlSchemaSet result = new XmlSchemaSet();
            result.XmlResolver = new XmlUrlResolver();
            /* If event handler doesn't set, errors and warnings of schema validation will be ignored */
            result.ValidationEventHandler += new ValidationEventHandler(SchemaValidationEventHandler);
            
            result.Add(null, HttpContext.Current.Server.MapPath(REFERENCED_SCHEMA));
            result.Add(null, HttpContext.Current.Server.MapPath(EVENT_SCHEMA));
            //result.Add(null, HttpContext.Current.Server.MapPath(BUSINESS_TX_SCHEMA));
            result.Compile();

            return result;
        }

        private static void SchemaValidationEventHandler(object sender, ValidationEventArgs e)
        {
            throw new SednaException(String.Format("Schema Validation: {0}", e.Message));
        }
    }
}
